package com.gxa.controller.investment;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.Result;
import com.gxa.dto.investmentDto.CompanyDto;
import com.gxa.entity.investment.*;
import com.gxa.service.investment.BusinessTypeChildService;
import com.gxa.service.investment.BusinessTypeService;
import com.gxa.service.investment.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 企业表 前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-16 11:39:46
 */
@Api(tags = "企业管理/企业档案")
@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;
    @Autowired
    private BusinessTypeService businessTypeService;
    @Autowired
    private BusinessTypeChildService businessTypeChildService;

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("列表接口")
    @GetMapping("/list/{pageNum}/{pageSize}")
    public Result<PageInfo> seletAll(@PathVariable("pageNum") Integer pageNum,
                                     @PathVariable("pageSize") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);

        PageInfo<Company> pageInfo = new PageInfo<>(companyService.seletAll());

        return Result.success(pageInfo);
    }


    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("按条件查询")
    @PostMapping("/query")
    public Result query(Integer pageNum, Integer pageSize, @RequestBody CompanyDto companyDto){
        try {
            PageInfo result = companyService.queryByCompanyDto(companyDto, pageNum, pageSize);

            return Result.success(result);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("query failed");
        }

    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("添加企业")
    @PostMapping("/add")
    public Result add(@RequestBody Company company){
        int results = companyService.add(company);
        if (results == 1){
            return Result.success("add success");
        }else {
            return Result.failed("add failed");
        }
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("删除企业")
    @DeleteMapping("/del/{id}")
    public Result delete(@PathVariable("id") Integer id){
        int results = companyService.delete(id);
        if (results == 1){
            return Result.success("del success");
        }else {
            return Result.failed("del failed");
        }

    }


    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("编辑企业")
    @PutMapping("/update")
    public Result update(@RequestBody Company company){
        int results = companyService.update(company);
        if (results == 1){
            return Result.success("update success");
        }else {
            return Result.failed("update failed");
        }
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("按id查询")
    @GetMapping("/query/{id}")
    public Result queryById(@PathVariable Integer id){
        Company results = companyService.queryById(id);
        if (results != null){
            return Result.success(results);
        }else {
            return Result.failed("查询失败");
        }
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("行业大类下拉选择")
    @GetMapping("/selet/business")
    public Result seletBusiness(){

        try {
            Set<String> results = businessTypeService.seletBusiness();
            if (results != null){
                return Result.success(results);
            }else {
                return Result.failed("查询失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("行业小类下拉选择")
    @GetMapping("/selet/business/child")
    public Result seletBusinessChild(){
        try {
            List<String> results = businessTypeChildService.seletChildName();
            if (results != null){
                return Result.success(results);
            }else {
                return Result.failed("查询失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

}
